<template>
  <div class="art-pub-container">
    <el-form ref="pubRef" label-width="100px" :model="pubForm" :rules="rules">
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="pubForm.title"
          placeholder="请填写文章标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select v-model="pubForm.cate_id" placeholder="请选择分类" clearable>
          <el-option :label="item.cate_name" :value="item.id" v-for="item in cateList" :key="item.id"> </el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="文章内容">
          <quill-editor v-model="pubForm.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面">
        <div class="cover-box">
         <img :src="pubForm.cover_img" alt="" class="cover-img" />
          <el-link type="primary" :underline="false" @click="$refs.iptFile.click()">+ 选择封面</el-link>
          <input type="file" ref="iptFile" accept="image/*"  hidden @change="handleChange"/>

        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="publish('已发布')">发布文章</el-button>
        <el-button type="info" @click="publish('草稿')">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 本地路径使用图片,webPack打包时不认识本地路径
// 自己监听change事件 完成前端本地预览 URL.creart

import { mapState } from 'vuex'
import defaultImg from '@/assets/images/cover.jpg'
import { reqPublishArticle } from '@/api/article'

export default {
  name: 'ArtPub',
  data () {
    return {
      pubForm: {
        title: '',
        cate_id: '',
        content: '',
        state: '',
        cover_img: defaultImg

      },
      file: null,

      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: ['change', 'blur'] },
          { min: 1, max: 30, message: '文章标题1-30字符', trigger: ['change', 'blur'] }
        ],
        cate_id: [
          { required: true, message: '文章分类不能为空', trigger: ['change', 'blur'] }
        ]
      }
      // imgUrl: defaultImg
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  methods: {
    handleChange (e) {
      const file = e.target.files[0]
      if (file) {
        const tempPath = URL.createObjectURL(file)
        console.log(tempPath)
        this.pubForm.cover_img = tempPath
        this.file = file
      }
    },
    publish (str) {
      this.pubForm.state = str

      this.$refs.pubRef.validate(async (flag) => {
        if (!flag) return console.log('校验失败')

        const fm = new FormData()
        fm.append('title', this.pubForm.title)
        fm.append('cate_id', this.pubForm.cate_id)
        fm.append('content', this.pubForm.content)
        fm.append('state', this.pubForm.state)
        fm.append('cover_img', this.file)

        const { data } = await reqPublishArticle(fm)
        console.log(data)
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        // 子传父派发事件
        this.$emit('pubDialogColse')
      })
    }
  }
}
// 前端本地预览(不经过服务器)
// window.onload = function () {
//   const inp = document.querySelector('input')
//   const img = document.querySelector('img')

//   inp.addEventListener('change', (e) => {
//     const file = e.target.files[0]
//     // 传递一个文件对象, 生成临时路径(创建该路径的页面一旦被关闭了, 就失效了)
//     const tempPath = URL.createObjectURL(file)
//     console.log(tempPath)
//     img.src = tempPath
//   })
// }
//

</script>

<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>
